package com.example.wishstar.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wishstar.MainActivity;
import com.example.wishstar.R;
import com.example.wishstar.databinding.ActivityOldBinding;
import com.example.wishstar.model.OldHabit;
import com.example.wishstar.model.OldWish;
import com.example.wishstar.view.adpter.OldHabitAdapter;
import com.example.wishstar.view.adpter.OldWishAdapter;
import com.example.wishstar.viewmodel.OldViewModel;

import java.util.List;

/**
 * 已归档Activity
 */
public class OldActivity extends AppCompatActivity implements OldHabitAdapter.OnOldHabitItemClickListener, OldWishAdapter.OnOldWishItemClickListener{

    private String TAG = "OldActivity";
    private ActivityOldBinding activityOldBinding;
    private OldViewModel oldViewModel;
    private LinearLayoutManager oldHabitManager;
    private LinearLayoutManager oldWishManager;
    private OldHabitAdapter oldHabitAdapter;
    private OldWishAdapter oldWishAdapter;
    private SwipeRefreshLayout swipeRefresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityOldBinding = DataBindingUtil.setContentView(this, R.layout.activity_old);
        oldViewModel = new ViewModelProvider(this).get(OldViewModel.class);

        oldHabitManager = new LinearLayoutManager(this);
        activityOldBinding.oldHabitRecyclerview.setLayoutManager(oldHabitManager);

        oldWishManager = new LinearLayoutManager(this);
        activityOldBinding.oldWishRecyclerview.setLayoutManager(oldWishManager);

        oldViewModel.getOldHabit().observe(this, new Observer<List<OldHabit>>() {
            @Override
            public void onChanged(List<OldHabit> oldHabits) {
                if (oldHabits.size() >= 0) {
                    Log.e(TAG, "observe-> oldHabits ========================>");
                    for (int i = 0; i < oldHabits.size(); ++i) {
                        Log.e(TAG, oldHabits.get(i).toString());
                    }
                    oldHabitAdapter = new OldHabitAdapter(oldHabits, OldActivity.this);
                    oldHabitAdapter.setOnOldHabitItemClickListener(OldActivity.this);
                    activityOldBinding.oldHabitRecyclerview.setAdapter(oldHabitAdapter);
                }
            }
        });

        oldViewModel.getOldWish().observe(this, new Observer<List<OldWish>>() {
            @Override
            public void onChanged(List<OldWish> oldWishList) {
                if (oldWishList.size() >= 0) {
                    Log.e(TAG, "observe-> oldWishList ========================>");
                    for (int i = 0; i < oldWishList.size(); ++i) {
                        Log.e(TAG, oldWishList.get(i).toString());
                    }
                    oldWishAdapter = new OldWishAdapter(oldWishList, OldActivity.this);
                    oldWishAdapter.setOnOldWishItemClickListener(OldActivity.this);
                    activityOldBinding.oldWishRecyclerview.setAdapter(oldWishAdapter);
                }
            }
        });

        swipeRefresh = activityOldBinding.oldSwipeRefresh;
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                oldViewModel.refresh();
                swipeRefresh.setRefreshing(false);
            }
        });

        TextView title = findViewById(R.id.back_title);
        title.setText(getString(R.string.old_habit_title));

        ImageView backImage = findViewById(R.id.image_title_back);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OldActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    @Override
    public void onLongItemClick(OldHabit oldHabit) {
        oldViewModel.deleteHabitByHabitId(oldHabit.habitId);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                oldViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }

    @Override
    public void onLongItemClick(OldWish oldWish) {
        oldViewModel.deleteWishByWishId(oldWish.wishId);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                oldViewModel.refresh();
            }
        }, 500);//0.5秒后执行Runnable中的run方法
    }
}